
# 下载LXML库
# PS E:\reptile-python> pip install lxml
# Requirement already satisfied: lxml in c:\program files\python310\lib\site-packages (4.8.0)

# 导包
import lxml.etree
from lxml import etree
import urllib.request

# 读取本地文件解析
# HTML_TREE = lxml.etree.parse("./xxx.html")

BASE_URL = 'https://pic.netbian.com/'
HEADER = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/103.0.0.0 Safari/537.36',
}
ENCODE1 = 'UTF-8'
ENCODE2 = 'GB2312'
ENCODE3 = 'GBK'

request = urllib.request.Request(url=BASE_URL, headers=HEADER)
response = urllib.request.urlopen(request)
# html = response.read().decode(ENCODE2)
html = response.read()


# 读取网页
HTML_TREE = etree.HTML(html)
srcList = HTML_TREE.xpath('//span/img/@src')
print(srcList)
for srcPath in srcList:
    fullPath = BASE_URL + srcPath
    idx = fullPath.rindex("/") + 1
    fileName = fullPath[idx:]
    print(fileName)
    urllib.request.urlretrieve(url=fullPath, filename=f'images/{fileName}')


